faApp.controller('FpAccountVoucherCtrl', ['$cookieStore','$scope','$routeParams','$location','productService','fpAccountVoucherService', '$controller', function($cookieStore,$scope,$routeParams, $location,productService,fpAccountVoucherService, $controller) {
  $controller('BaseListCtrl', {
    $scope: $scope
  });

  $scope.showFullName = function(){
      // 根据name查询列表
       var pName= $scope.querypFullName==undefined?'':$scope.querypFullName;

      productService.queryAllProductByFullName({'pName':pName}).then(function(json){
        $scope.productFullName = json.map;
        $scope.loading = false;
      });
  }

  $scope.showAllExistingAccount = function(){
     var fundAccountName= $scope.queryExistingAccount==undefined?'':$scope.queryExistingAccount;

      fpAccountVoucherService.queryAllExistingAccount({'fundAccountName':fundAccountName}).then(function(json){
        $scope.allExistingAccount = json.map;
        $scope.loading = false;
      });
  }
  $scope.config = {"pageSize": 20,
    "pageNo": 1};
  $scope.search = function(){
    //查询凭条列表
    fpAccountVoucherService.listFpAccountVoucher($scope.config).then(function(json){
      $scope.accountVoucherList = json.list;
      $scope.pager=json.pager;
      $scope.loading = false;
    });
  }

  $scope.$watch('config',function(){
    $scope.search();
  },true);

  fpAccountVoucherService.queryAccountVoucherCreateBy({}).then(function(json){
    $scope.accountVoucherCreateBy = json.map;
    $scope.loading = false;
  });
 
  $scope.showFullName();
  $scope.showAllExistingAccount();
  $scope.search();
}]);



faApp.controller('FpAccountVoucherDetailCtrl', ['$cookieStore','$scope','$routeParams','$location','$route','productService','fpAccountVoucherService', '$controller', function($cookieStore,$scope,$routeParams, $location,$route,productService,fpAccountVoucherService, $controller) {
  $controller('BaseListCtrl', {
    $scope: $scope
  });

  $scope.accountVoucher={};

  $scope.productInfo =[{}];
  $scope.isShowExistingAccount=false;

  $scope.id=$routeParams.id;
  if($scope.id){
     fpAccountVoucherService.detailFpAccountVoucher({'id':$scope.id}).then(function(json){
        $scope.accountVoucher = json.dto;
         $scope.productInfo=$scope.accountVoucher.fpAccountVoucherProductList;
         //区分
         angular.forEach($scope.productInfo,function(o,i){
          if($scope.productInfo[i].accountType.indexOf("1")!=-1){
            $scope.productInfo[i].sale='0';
          }
          if($scope.productInfo[i].accountType.indexOf("2")!=-1){
            $scope.productInfo[i].tiexi='0';
          }
          if($scope.productInfo[i].accountType.indexOf("3")!=-1){
            $scope.productInfo[i].buxi='0';
          } 
           $scope.showFullName(i);
        });
        console.log( $scope.accountVoucher);
        $scope.loading = false;
      });
  }


  $scope.showFullName = function(v,queryName){
      // 根据name查询列表
      var pName= queryName==undefined?'':queryName;
      productService.queryAllProductByFullName({'pName':pName}).then(function(json){
        $scope.productInfo[v].productFullName = json.map;
        console.log($scope.productInfo[v].productFullName);
        $scope.loading = false;
      });
  }

  $scope.showAllExistingAccount = function(){

     var accountName= $scope.accountVoucher.fundAccountName==undefined?'':$scope.accountVoucher.fundAccountName;
      if(!accountName){
         $scope.isShowExistingAccount=false;
        return ;  
      }
      fpAccountVoucherService.queryAllExistingAccount({'fundAccountName':accountName}).then(function(json){
        $scope.allExistingAccount = json.map;
        if(JSON.stringify( $scope.allExistingAccount) != "{}"){
           $scope.isShowExistingAccount=true;
        }else{
          $scope.isShowExistingAccount=false;
        }
        console.log( $scope.allExistingAccount);
        $scope.loading = false;
      });
  }
   
 
  $scope.isShowExistingAccountClick = function(v){
    $scope.accountVoucher.fundAccountName=v;
    $scope.isShowExistingAccount=false;
  }


  $scope.showFullName(0);



  $scope.save = function(){
    if($scope.form.$invalid){
      alert("填写完整表单!");
      return;
    }
    console.log($scope.productInfo);
    var isEnd=false;
    angular.forEach($scope.productInfo,function(o,i){
        var sale=$scope.productInfo[i].sale;
        var tiexi=$scope.productInfo[i].tiexi;
        var buxi=$scope.productInfo[i].buxi;
        var code=$scope.productInfo[i].productCode;
        var value="";
        if(sale=="0"){
          value+="1";
        }
        if(tiexi=="0"){
          value+="2";
        }
        if(buxi=="0"){
          value+="3";
        }
        if(!value){
          alert("入账类型必填!");
          isEnd=true;
          return ;
        }
        if(!code){
           alert("产品必选!");
           isEnd=true;
          return ;
        }
        console.log(value);
        $scope.productInfo[i].accountType=value;
      });
    if(isEnd){
      return ;
    }
    //删除产品名字
    angular.forEach($scope.productInfo,function(o,i){
        delete $scope.productInfo[i].productFullName;
    });
    $scope.accountVoucher.productInfo=angular.toJson($scope.productInfo);

    if($scope.id){//编辑
       delete $scope.accountVoucher.fpAccountVoucherProductList;
       fpAccountVoucherService.updateFpAccountVoucher($scope.accountVoucher).then(function(json){
         if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          alert("编辑成功!!");
        }
        $route.reload();
        console.log(json);
      });
    }else{
       fpAccountVoucherService.addFpAccountVoucher($scope.accountVoucher).then(function(json){
         if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          alert("新增成功!!");
        }
         $route.reload();
        console.log(json);
      });
    }
  }
  //新增空对象
  $scope.addItem = function(){
    $scope.productInfo.push({});
    $scope.showFullName($scope.productInfo.length-1);
  }
  //删除对象
  $scope.deleteItem = function(v){
    $scope.productInfo.splice(v,1);
  }

  $scope.changeMoney = function(){
    $scope.accountVoucher.allMoney=0;
    angular.forEach($scope.productInfo,function(o,i){
        var accountMoney=$scope.productInfo[i].accountMoney;
        if(accountMoney){
          $scope.accountVoucher.allMoney+=parseFloat(accountMoney);
        }
      });
  }

   $scope.onUploadFile({
    showMessage: function(m) { alert(m); },
    onComplete: function(f, response, id) {
      if (response.statusCode == "0000") {
        $scope.accountVoucher.accountVoucherUrl = response.attribute.fileUrl;
        $scope.$apply();
      } else {
        alert('上传失败');
      }
    }
  });
}]);